package com.mh.common;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.mh.dao.AssetAccountDao;
import com.mh.dao.AssetChangeDao;
import com.mh.dto.vo.TransferVo;
import com.mh.pojo.AssetAccount;
import com.mh.pojo.AssetChange;
import com.mh.pojo.Bill;
import com.mh.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * Date:2025/2/11
 * author:zmh
 * description: MQ消息监听 & 处理器
 **/

@Component
@Slf4j
@RequiredArgsConstructor
@Transactional
public class RabbitMessageListener {

    // 处理用户添加账本
    private final UserService userService;

    // 资产账户操作
    private final AssetAccountDao assetAccountDao;

    // 资产变动表操作
    private final AssetChangeDao assetChangeDao;

    // 转账
    @RabbitListener(queues = "transfer")
    public void transfer(TransferVo  transferVo){
        log.info("MQ：转账，开始异步记录账户资金变动信息...");
        // 记录两个账户的资金变动
        // 记录转出
        AssetAccount assetAccountOfOut = assetAccountDao.selectById(transferVo.getFromId());
        // 构造资金变动数据对象
        AssetChange assetChange = new AssetChange(transferVo.getFromId(), transferVo.getAmount(), assetAccountOfOut.getBalance(), 3, 0, "转账到账户：" + transferVo.getToId());
        assetChangeDao.insert(assetChange);

        // 记录转入
        AssetAccount assetAccountOfIn = assetAccountDao.selectById(transferVo.getToId());
        // 构造资金变动数据对象
        AssetChange assetChange2 = new AssetChange(transferVo.getToId(), transferVo.getAmount(), assetAccountOfIn.getBalance(), 3, 0, "从账户：" + transferVo.getFromId() + " 转入");
        assetChangeDao.insert(assetChange2);
    }

    @RabbitListener(queues = "addBill")
    public void addBill(Bill bill){
        log.info("MQ：账单添加，开始异步处理账单资金变动信息...");
        // 变动资产账户的金额
        LambdaUpdateWrapper<AssetAccount> assetAccountLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        // 判断是支出还是收入操作 1: 收入 2：支出
        if(bill.getType() == 1){
            assetAccountLambdaUpdateWrapper.eq(AssetAccount::getId, bill.getAccountId())
                    .setSql("balance = balance + " + bill.getAmount());
        }else{
            assetAccountLambdaUpdateWrapper.eq(AssetAccount::getId, bill.getAccountId())
                    .setSql("balance = balance - " + bill.getAmount());
        }

        // 执行金额增减操作
        int update = assetAccountDao.update(null, assetAccountLambdaUpdateWrapper);
        if(update <= 0){
            log.info("账户{}，金额变动失败！", bill.getAccountId());
            return;
        }

        log.info("账户{}，金额变动成功！", bill.getAccountId());

        // 执行后查询出该账户的最新金额
        AssetAccount assetAccount = assetAccountDao.selectById(bill.getAccountId());

        // 构造资金变动数据对象并插入表中
        AssetChange assetChange = new AssetChange(bill.getAccountId(), bill.getAmount(), assetAccount.getBalance(), 1, bill.getId(), bill.getRemark());
        int insert = assetChangeDao.insert(assetChange);
        if(insert <= 0){
            log.info("账户{}，资金变动记录插入失败！", bill.getAccountId());
        }

        log.info("账户{}，资金变动记录插入成功！", bill.getAccountId());

    }

    // 给新用户添加默认账本
    @RabbitListener(queues = "addBookForNewUser")
    public void addDefaultBookForNewUser(String openId){
        log.info("MQ：新用户注册，开始给新用户添加默认账本...");
        if(!userService.addDefaultBookForNewUser(openId)){
            log.info("用户{}，默认账本添加失败！", openId);
        }else{
            log.info("用户{}，默认账本添加成功~", openId);
        }
    }
}
